﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;

namespace RTS
{
	public class GameMain
	{
		private List<ITimeDependant> timeDependants = new List<ITimeDependant>();
		private List<PosicionableObject> representables = new List<PosicionableObject>();

		private PosicionableObject selectedObject = null;

		public GameMain()
		{
			MainBuilding mainBuilding = new MainBuilding(this);
			mainBuilding.setPosition(new Position2D(250, 350));
			addBuilding(mainBuilding);

			LanceMan lanceMan = new LanceMan(this);
			lanceMan.setPosition(new Position2D(700, 700));
			addUnit(lanceMan);

			CrossBowMan crossBowMan = new CrossBowMan(this);
			crossBowMan.setPosition(new Position2D(400, 700));
			addUnit(crossBowMan);
		}

		public void addUnit(Unit unit)
		{
			representables.Add(unit);
			timeDependants.Add(unit);
		}

		public void addBuilding(Building building)
		{
			representables.Add(building);
			timeDependants.Add(building);
		}

		public List<PosicionableObject> getPosicionableContent()
		{
			return representables;
		}

		public void update()
		{
			for (int i = 0; i < timeDependants.Count; i++ )
			{
				timeDependants[i].update();
			}
		}

		public void selectObject(PosicionableObject selectedObject)
		{
			unselectObject(this.selectedObject);

			this.selectedObject = selectedObject;
			this.selectedObject.select();
		}

		public void unselectObject(PosicionableObject objectToUnselect)
		{
			if (this.selectedObject != null && this.selectedObject == objectToUnselect)
			{
				this.selectedObject.unselect();
				this.selectedObject = null;
			}
		}

		public PosicionableObject getSelectedObject()
		{
			return selectedObject;
		}

	}
}
